package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.ClickFastUtil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/11.
 */

public class AddBankCardActivity extends BaseBackActivity {

    @BindView(R.id.et_chikaren)
    AutoCompleteTextView etChikaren;
    @BindView(R.id.et_xuanzhekaihuhang)
    AutoCompleteTextView etXuanzhekaihuhang;
    @BindView(R.id.et_yinhangkahao)
    AutoCompleteTextView etYinhangkahao;
    @BindView(R.id.et_zhihang)
    AutoCompleteTextView etZhihang;
    @BindView(R.id.publish)
    TextView publish;
    @BindView(R.id.cb_sheweimoren)
    CheckBox cbSheweimoren;
    private int classType;

    public static void show(Context context) {
        IntentUtil.intent2Activity(context, AddBankCardActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitle("添加银行卡");
        Drawable drawable=getResources().getDrawable(R.drawable.checkbox_select);
        drawable.setBounds(0,0,40,40);
        cbSheweimoren.setCompoundDrawables(drawable,null,null,null);
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_addnewbank;
    }


    @OnClick(R.id.publish)
    public void onViewClicked() {
        if (ClickFastUtil.isFastClick3000()&&CheckUtil.checkText(getActivity(), etChikaren, etXuanzhekaihuhang, etYinhangkahao, etZhihang)) {
                save();
        }
    }

    private void save() {
        ParamsUtil params = new ParamsUtil();
        params.put("fdBankUserName", etChikaren);
        params.put("fdBankName", etXuanzhekaihuhang);
        params.put("fdBankNo", etYinhangkahao);
        params.put("fdBranchName", etZhihang);
        params.put("fdStatus", cbSheweimoren.isChecked()?1:0);

        HTTPUtils.post(getActivity(), Constance.getSavebankURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                EventBus.getDefault().post(new MessageEvent(MessageAction.REFRESHBANK));
                SyDialogHelper.showDlg(getActivity(), "提示", "成功添加银行卡", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        finish();
                    }
                });
            }
            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
